package m.jclecai.com.fragment;


import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.LotteryAnnouncementAdapter;
import m.jclecai.com.base.BaseFragment;
import m.jclecai.com.bean.OpenCodeBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.contents.LotteryType;
import m.jclecai.com.inter.OnResultCallback;
import m.jclecai.com.ui.LotteryDetailsActivity;
import m.jclecai.com.ui.LotteryHistoryActivity;
import m.jclecai.com.ui.SFC_LotteryDetails;
import m.jclecai.com.ui.ZQ_LQ_LotteryDetailsActivity;
import m.jclecai.com.utils.OkHttpClientManager;
import m.jclecai.com.utils.ToastUtil;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.view.AutoSwipeRefreshLayout;


/**
 * 开奖信息
 */
public class LotteryFragment extends BaseFragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    @BindView(R.id.lv_list)
    ListView lvList;
    @BindView(R.id.sr_refresh)
    AutoSwipeRefreshLayout refreshLayout;
    private LotteryAnnouncementAdapter adapter;
    private List<OpenCodeBean> bean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_lottery;
    }

    @Override
    public void initView() {
        super.initView();

        //setTitle("开奖信息");
        bean = new ArrayList<>();
        adapter = new LotteryAnnouncementAdapter(getActivity(), bean);
        lvList.setAdapter(adapter);
        lvList.setOnItemClickListener(this);
        initLoadingLayout(refreshLayout);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getOpenCodeData();
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        refreshLayout.setRefreshing(true);
        getOpenCodeData();
    }

    //获取开奖公告数据
    private void getOpenCodeData() {
        try {
            OkHttpClientManager.getInstance().asynGet(new OnResultCallback() {
                @Override
                public void onGetResult(String json) {
                    closeLoadingLayout();
                    if (refreshLayout != null)
                        refreshLayout.setRefreshing(false);
                    bean.clear();
                    bean.addAll(FastOk.getList(json, OpenCodeBean.class));
                    if (bean.size() <= 0) {
                        showEmptyLayout();
                    }
                    adapter.notifyDataSetChanged();
                }

                @Override
                public void onError() {
                    closeLoadingLayout();
                    if (refreshLayout != null)
                        refreshLayout.setRefreshing(false);
                    if (bean.size() <= 0)
                        showExceptionLayout();
                    ToastUtil.showToast(getActivity(), R.string.connect_failure);
                }
            }, Constant_Url.OPEN_CODE);
        } catch (IOException e) {
            dissDialog();
            e.printStackTrace();
        }
    }


    @Override
    public void onClick(View v) {
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if(refreshLayout != null) refreshLayout.setRefreshing(false);
        TextView tv_lotteryName = (TextView) view.findViewById(R.id.tv_lotteryName);
        LotteryAnnouncementAdapter.ViewHolder viewHolder = (LotteryAnnouncementAdapter.ViewHolder) view.getTag();

        String lotteryid = tv_lotteryName.getTag().toString();

        TextView tv_date = (TextView) view.findViewById(R.id.tv_date);
        String date = tv_date.getTag().toString();

        TextView tv_ball_one = (TextView) view.findViewById(R.id.tv_ball_one); //期号
        String term = tv_ball_one.getTag().toString();

        TextView tv_ball_two = (TextView) view.findViewById(R.id.tv_ball_two); //彩种
        String type = tv_ball_two.getTag().toString();

        int i = Integer.parseInt(lotteryid);
        if (i == LotteryType.JCZQ || i == LotteryType.JCLQ || i == LotteryType.BJDC) {
            Intent intent = new Intent(getActivity(), ZQ_LQ_LotteryDetailsActivity.class);
            intent.putExtra("type", type);
            intent.putExtra("typeName",viewHolder.tv_lotteryName.getText().toString());
            startActivity(intent);
        } else if (i == LotteryType.SFC) {
            Intent intent = new Intent(getActivity(), SFC_LotteryDetails.class);
            intent.putExtra("type", type);
            intent.putExtra("term", term);
            intent.putExtra("typeName",viewHolder.tv_lotteryName.getText().toString());
            startActivity(intent);
        }
    }

    @Override
    public void OnRetry(String btnMessage) {
        refreshLayout.setRefreshing(true);
        closeLoadingLayout();
        getOpenCodeData();
    }
}
